<?php
/**
 * Created by PhpStorm.
 * User: Chris
 * Date: 14-4-17
 * Time: 上午11:36
 */


use Orm\Model;

class Model_BaseModel extends Model
{
    const DEFAULT_TX = "master_db";
    protected static $_write_connection = 'master_db';
    protected static $_connection = 'slave_db';


    const STATUS_VALID= 'VALID';
    const STATUS_PENDING= 'PENDING';
    const STATUS_INVALID= 'INVALID';

    protected static function currentDatetime(){
        date_default_timezone_set("PRC");
        return date('Y-m-d H:i:s');
    }


    public static function createValidation($factory){
        $val = Validation::forge($factory);

        $val->set_message('required', "请输入 :label");
//        $val->set_message('valid_string', " :label 长度不能超过 :param:1 个");
//        $val->set_message('max_length', ":label 长度不能超过 :param:1 个（每个汉字长度是3个)");
        $val->set_message('max_length', ":label 长度不能超过 :param:1 个（每个汉字长度是3个)");
        $val->set_message('valid_string', ":label 类型有错，必须为 :param:1");

//        $val->error('ordering')->set_message(":label 必须为数字类型");

        return $val;
    }

    public function statusStr(){
        if ($this->status==self::STATUS_VALID){
            return "有效";
        }else if ($this->status==self::STATUS_INVALID){
            return "无效";
        }else if ($this->status==self::STATUS_PENDING){
            return "等待审核";
        }
        return $this->status;
    }

    public static function findAll($whereOptions, $orderOptions,
                                   $pagination,
                                   $size=10, $count_total=true,
                                   $related=null, $from_cache=true,
                                   $group_by=null){
        $ret = self::find_all($whereOptions, $orderOptions,
            $pagination,
            $size, $count_total,
            $related, $from_cache,
            $group_by);
        $list = $ret['list'];
        $count= $ret['count'];
        return array($list, $count);
    }


    /**
     * @param array $whereOptions 查询条件 array('field'=>1, array('id','in', array(1,2))
     * @param array $orderOptions 排序条件 array('createtime'=>'desc')
     * @param int $pagination 分页pagination 或 下标
     * @param int $size 每页记录数
     * @param bool $count_total 是否计算总记录数
     * @param array $related 关联对象
     * @param bool $from_cache
     * @param $group_by
     * @return array 结果，返回 array('list'=>结果集, 'count'=>总记录数)
     */
    public static function find_all(array $whereOptions, array $orderOptions=null,
                                    $pagination=0,
                                    $size=10, $count_total=true,
                                    array $related=null, $from_cache=true,
                                    $group_by=null
    ){
        $query = self::query();
        if (!isset($whereOptions)){
            $whereOptions = array();
        }

        if (isset($whereOptions)&&!empty($whereOptions)){
            $query->where($whereOptions);
        }
        if (isset($group_by)&&$group_by){
            $query->group_by($group_by);
        }

        if (isset($orderOptions)){
            $query->order_by($orderOptions);
        }else{
            $query->order_by("createtime", "desc");
        }
        if (isset($related) && $related!=null){
            $query->related($related);
        }

        if ($count_total){
            $count = $query->count(false, false);
//            $count = $query->count('id', false);
        }else{
            $count = 0;
        }
        if ($pagination instanceof \Fuel\Core\Pagination){
            $pagination->total_items = $count;
            $size = $pagination->per_page;

            $offset = $pagination->offset;
//            $offset = $pagination->per_page;
//            echo "1";exit;
        }else{
            $offset = $pagination;
//            echo "2";exit;
        }

//        echo "offset=".$offset;exit;
        $list = $query
            ->from_cache($from_cache)
//                    ->offset($offset)
            ->rows_offset($offset) // must use row_offset
//                    ->limit($size)
            ->rows_limit($size)  // must use row_limit
            ->get();
        $sql = $query->get_query();
//        echo "SQL:".;
//

        return array(
            'count'=> $count,
            'list'=> $list,
//            'sql'=> &$sql,
        );

    }

}